/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package selectjava;
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
 *
 * @author Usuario
 */
public class SelectJava implements ActionListener{
 
    JLabel jlOpMen, jlOp, jlRs;
    Choice chMenu, chN1, chN2, chEj2;
    JButton jbObt;
    JPanel jpMenu, jpEj1, jpEj2;
    JFrame winMain;
    int n1, n2;
 
    public SelectJava(){
 
        winMain = new JFrame("Usar Choice");        
        winMain.setLayout(new BorderLayout(4, 4));
 
        panelMenu();
 
        winMain.add(jpMenu, BorderLayout.WEST);
 
        winMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        winMain.setSize(450, 300);
        winMain.setResizable(false);
        winMain.setVisible(true);
    }
 
    public void panelMenu(){
        jpMenu = new JPanel();
        jpMenu.setLayout(new FlowLayout(55, 3, 45));
 
        String arStr [] = {" ", "Suma", "Seleccion"}; //arreglo con las opciones
 
        jlOpMen = new JLabel("Opciones ->");
        chMenu  = new Choice();
        chMenu.setSize(70, 25);
 
        for(int i=0; i<arStr.length; i++){//ciclo para llenar el choice con las opciones
            chMenu.add(arStr[i]); 
        }
 
        jpMenu.add(jlOpMen);
        jpMenu.add(chMenu);
 
        evenChoice obCh = new evenChoice();
 
        chMenu.addItemListener(obCh);   
 
        jpMenu.setSize(100, 100);
        jpMenu.setVisible(true);
    }
 
 //prueba de modificación
    public void panelEjem1(){//panel para la opcion de resta
        jpEj1 = new JPanel();         
 
        jlOp            = new JLabel("RESTA");
        chN1            = new Choice();
        JLabel jlSim    = new JLabel(" - ");
        chN2            = new Choice();
        jlRs            = new JLabel("");
 
        for(int i=0; i<10; i++){
            chN1.add(""+i); chN2.add(""+i);
        }
 
        jlOp.setBounds(15, 10, 70, 25);
        chN1.setBounds(15, 40, 70, 25); jlSim.setBounds(85, 10, 70, 25); chN2.setBounds(155, 10, 70, 25);
        jlRs.setBounds(15, 90, 70, 25);
 
        evenChoice obCh = new evenChoice(); //creacion objeto clase listener choice
 
        chN1.addItemListener(obCh); chN2.addItemListener(obCh); //añadiendo evento al choice
 
        jpEj1.add(jlOp); jpEj1.add(chN1); jpEj1.add(jlSim); jpEj1.add(chN2); jpEj1.add(jlRs);
 
        jpEj1.setSize(120, 100);
        jpEj1.setVisible(false);
 
    }
 
    public void jpEj2(){
        jpEj2 = new JPanel();
 
        String arr [] = {" ", "Chile", "Colombia", "Mexico", "Venezuela"};
 
        jlOp = new JLabel("Seleccion");
        chEj2 = new Choice();
        jbObt   = new JButton("Obtener!");
 
        for (int i = 0; i < arr.length; i++) {
            chEj2.add(arr[i]);
        }
 
        jpEj2.add(jlOp); jpEj2.add(chEj2); jpEj2.add(jbObt);
 
        jbObt.addActionListener(this);
 
        jpEj2.setSize(130, 100);
        jpEj2.setVisible(false);        
    }
 
    @Override
    
    //esto es comentario de prueba 
    public void actionPerformed(ActionEvent e) {//evento para el boton del segundo ejemplo
 
        if(!chEj2.getSelectedItem().equals("")){
            JOptionPane.showMessageDialog(null, chEj2.getSelectedItem());
        }else{
            JOptionPane.showMessageDialog(null, "Selecciona algun valor");
        }
    }
 
    private class evenChoice implements ItemListener{//clase para cambio del choice
        @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getSource()==chMenu && chMenu.getSelectedItem().equals("Suma")){//si se selecciona producto se configura lo siguiente
            if(jpEj2 != null && jpEj2.isVisible()){
 
                    jpEj2.setVisible(false);
                    panelEjem1();
                    winMain.add(jpEj1, BorderLayout.EAST);
                    jpEj1.setVisible(true);
            }else if(jpEj2==null){
                    panelEjem1();
                    winMain.add(jpEj1, BorderLayout.EAST);
                    jpEj1.setVisible(true);
            }        
        }if(e.getSource()==chMenu && chMenu.getSelectedItem().equals("Seleccion")){//si se selecciona producto se configura lo siguiente
            if(jpEj1 != null && jpEj1.isVisible()){
 
                    jpEj1.setVisible(false);
                    jpEj2();
                    winMain.add(jpEj2, BorderLayout.EAST);
                    jpEj2.setVisible(true);
            }else if(jpEj1==null){
                    jpEj2();
                    winMain.add(jpEj2, BorderLayout.EAST);
                    jpEj2.setVisible(true);
            }        
        }else if(e.getSource()==chN1){//evento del choice para el primer numero
            n1 = Integer.parseInt(chN1.getSelectedItem());
        }else if(e.getSource()==chN2){//evento del choice para el segundo numero
            n2 = Integer.parseInt(chN2.getSelectedItem());
            jlRs.setText(""+(n1+n2)); 
        }            
        }        
    }
 
    public static void main(String[] args) {
 
        SelectJava ob = new SelectJava();
    }
}